package com.example.mgs.config;

import com.example.common.constants.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// message/config/FileConfig.java
@Configuration
public class FileConfig {
    // 主题交换机
    public static final String EXCHANGE_FILE = MqConstants.EXCHANGE_FILE;

    // 文件处理队列
    public static final String QUEUE_FILE_SAVE = MqConstants.QUEUE_FILE_SAVE;
    public static final String QUEUE_FILE_DELETE = MqConstants.QUEUE_FILE_DELETE;
    public static final String QUEUE_FILE_UPDATE = MqConstants.QUEUE_FILE_UPDATE;

    //图片
    public static final String QUEUE_FILE_THUMBNAIL = MqConstants.QUEUE_FILE_THUMBNAIL;

    // 路由键
    public static final String ROUTING_KEY_FILE_SAVE = MqConstants.ROUTING_KEY_FILE_SAVE;
    public static final String ROUTING_KEY_FILE_DELETE = MqConstants.ROUTING_KEY_FILE_DELETE;
    public static final String ROUTING_KEY_FILE_UPDATE = MqConstants.ROUTING_KEY_FILE_UPDATE;

    //图片路由键
    public static final String ROUTING_KEY_IMAGE_UPLOAD = MqConstants.ROUTING_KEY_IMAGE_UPLOAD;

    //分片
    public static final String ROUTING_KEY_FILE_MERGE = MqConstants.ROUTING_KEY_FILE_MERGE;
    public static final String QUEUE_FILE_MERGE = MqConstants.QUEUE_FILE_MERGE;

    @Bean
    public TopicExchange fileExchange() {
        return new TopicExchange(EXCHANGE_FILE, true, false);
    }

    @Bean
    public Queue fileSaveQueue() {
        return new Queue(QUEUE_FILE_SAVE, true);
    }

    @Bean
    public Queue fileUpdateQueue() {
        return new Queue(QUEUE_FILE_DELETE, true);
    }

    @Bean
    public Queue fileDeleteQueue() {
        return new Queue(QUEUE_FILE_UPDATE, true);
    }

    @Bean
    public Queue fileThumbnailQueue() {
        return new Queue(QUEUE_FILE_THUMBNAIL, true);
    }

    @Bean
    public Queue fileMergeQueue() {
        return new Queue(QUEUE_FILE_MERGE, true);
    }

    @Bean
    public Binding fileSaveBinding() {
        // 处理所有文件上传
        return BindingBuilder.bind(fileSaveQueue())
                .to(fileExchange())
                .with(ROUTING_KEY_FILE_SAVE);
    }

    @Bean
    public Binding fileDeleteBinding() {
        // 处理所有文件删除
        return BindingBuilder.bind(fileDeleteQueue())
                .to(fileExchange())
                .with(ROUTING_KEY_FILE_DELETE);
    }

    @Bean
    public Binding fileUpdateBinding() {
        // 处理所有文件修改
        return BindingBuilder.bind(fileUpdateQueue())
                .to(fileExchange())
                .with(ROUTING_KEY_FILE_UPDATE);
    }

    @Bean
    public Binding fileMergeBinding() {
        // 处理分片文件
        return BindingBuilder.bind(fileUpdateQueue())
                .to(fileExchange())
                .with(ROUTING_KEY_FILE_MERGE);
    }

    @Bean
    public Binding fileThumbnailBinding() {
        // 只处理图片上传
        return BindingBuilder.bind(fileThumbnailQueue())
                .to(fileExchange())
                .with(ROUTING_KEY_IMAGE_UPLOAD);
    }
}
